Common Alpha Factors

Load Data

TA-Lib: Function Groups

List number of available functions by group

Function Group # Indicators
Overlap Studies 17
Momentum Indicators 30
Volume Indicators 3
Volatility Indicators 3
Price Transform 4
Cycle Indicators 5
Pattern Recognition 61
Math Operators 11
Math Transform 15
Statistic Functions 9

The documentation for TA-Lib's Python interface is quite limited; you can find the most accurate information on the various functions in the doc strings of the C source code

Unstable periods

Some TA-Lib functions depend on all past data and are sometimes called functions with memory. For example, the Exponential Moving Average (EMA), recursively uses the previous EMA in its computation. As a result, a given data point will continue to have an influence on all subsequent values. In contrast, a simple moving average (SMA) only reflects the values in a finite window of the data.

The following graphic shows three EMA, all being configured for 9 periods. The only difference is that they start their processing at different point in time. Notice that eventually they stabilize and overlap.

https://ta-lib.org/d_api/ta_setunstableperiod.html

Moving Averages

Numerous indicators allow for calculation using different types of moving average

MA Type Name Code
SMA Simple Moving Average 0
EMA Exponential Moving Average 1
WMA Weighted Moving Average 2
DEMA Double Exponential Moving Average 3
TEMA Triple Exponential Moving Average 4
TRIMA Triangular Moving Average 5
KAMA Kaufman Adaptive Moving Average 6
MAMA MESA Adaptive Moving Average 7
T3 Triple Exponential Moving Average 8

Simple Moving Average (SMA)

For price series $P_t$ with a window of length $N$, the simple moving average at time $t$ weighs each data point within the window has equal weight: $$\text{SMA}(N)_t= \frac{P_{t-N+1}+P_{t-N+2}+P_{t-N+3}+P_{t}}{N}=\frac{1}{N}\sum_{i=1}^N P_{t-N+i}$$

Exponential Moving Average (EMA)

For price series $P_t$ with a window of length $N$, the exponential moving average at time $t$ $EMA_t$ is recursively defined as the weighted average of the current price and most recent previous $EMA_{t-1}$, where the weights $\alpha$ and $1-\alpha$ are defined as below: $$\begin{align*} \text{EMA}(N)_t&= \alpha P_t + (1-\alpha)\text{EMA}(N)_{t-1} \\ \alpha&=\frac{2}{N+1}\\ \end{align*}$$

Weighted Moving Average (WMA)

For price series $P_t$ with a window of length $N$, the weighted moving average at time $t$ is computed as such that the weight of each data point corresponds to its index within the window: $$\text{WMA}(N)_t= \frac{P_{t-N+1}+2P_{t-N+2}+3P_{t-N+3}+NP_{t}}{N(N+1)/2}$$

Double Exponential Moving Average (DEMA)

The double exponential moving average for a price series $P_t$ at time $t$, $\text{DEMA}_t$ is based on the EMA yet designed to react faster to changes in price. I t is computed as the difference between twice the current EMA and the EMA applied to the current EMA, labeled $\text{EMA}_2(N)_t$:

$$\text{DEMA}(N)_t=2\times\text{EMA}(N)_t-\text{EMA}_2(N)_t$$

Since the calculation uses $\text{EMA}_2$, DEMA needs 2 * N - 1 samples to start producing values.

Triple Exponential Moving Average (TEMA)

The triple exponential moving average for a price series $P_t$ at time $t$, $\text{TEMA}_t$ is based on the EMA yet designed to react faster to changes in price and indicate short-term price direction. It is computed as the difference between three time the difference between the current EMA and the EMA applied to the current EMA, $\text{EMA}_2$, with addition of the EMA of the $\text{EMA}_2$, labeled as $\text{EMA}_3$:

$$\text{TEMA}(N)_t=3\times\left[\text{EMA}(N)_t-\text{EMA}_2(N)_t\right] + \text{EMA}_3(N)_t$$

Since the calculation uses $\text{EMA}_3$, DEMA needs 3 * N - 2 samples to start producing values.

Triangular Moving Average (TRIMA)

The triangular moving average (TRIMA) with window length N for a price series $P_t$ at time $t$, $\text{TRIMA}(N)_t$ is a weighted average of the last N $SMA(N)_t$ values. In other words, it applies the SMA to a time series of SMA values:

$$\text{TRIMA}(N)_t=\frac{1}{N}\sum_{i=1}^N \text{SMA}(N)_{t-N+i}$$

Since the calculation applies the SMA(N) to the SMA(N), TRIMA needs 2 * N - 1 samples to start producing values.

Kaufman Adaptive Moving Average (KAMA)

The computation of the Kaufman adaptive moving average aims to take into account changes in market volatility. See here for details on the slightly more involved computation.

MESA Adaptive Moving Average (MAMA)

The MESA Adaptive Moving Average (MAMA) is an exponential moving average that adapts to price movement based on the rate change of phase as measured by the Hilbert Transform Discriminator (see below). In addition to the price series, MAMA accepts two additional parameters, fastlimit and slowlimit that control the maximum and minimum alpha value that should be applied to the EMA using when calculating MAMA.

Comparison

Overlap Studies

Function Name
BBANDS Bollinger Bands
HT_TRENDLINE Hilbert Transform - Instantaneous Trendline
MAVP Moving average with variable period
MA Moving average
MIDPOINT MidPoint over period
MIDPRICE Midpoint Price over period
SAR Parabolic SAR
SAREXT Parabolic SAR - Extended

Bollinger Bands

Normalized squeeze & mean reversion indicators

Visualize Distribution

Plot outlier price series

Hilbert Transform - Instantaneous Trendline

The Hilbert Transform generates inphase and quadrature components of a de-trended real-valued signal like a price series to analyze variations of the instantaneous phase and amplitude.

HTTrendline returns the price series value after the Dominant Cycle of the analytic signal as generated by the Hilbert Transform has been removed. The Dominant Cycle can be thought of as being the "most likely" period (in the range of 10 to 40) of a sine function of the Price Series. See here for additional detail on computation.

Compute Hilbert-based normalized indicator

Parabolic SAR

The Parabolic SAR aims to identify trend reversals. It is a trend-following (lagging) indicator that can be used to set a trailing stop loss or determine entry or exit points. It is usually represented in a price chart as a set of dots near the price bars. Generally, when these dots are above the price, it signals a downward trend, and an upward trend if the dots are below the price. The change in the direction of the dots can be interpreted as a trade signal. However, the indicator is not as reliable in a flat or range-bound markt. It is compute as follows:

$$\text{SAR}_t=\text{SAR}_{t-1} + \alpha (\text{EP}-\text{SAR}_{t-1})$$

EP (the extreme point) is a record kept during each trend that represents the highest value reached by the price during the current uptrend – or lowest value during a downtrend. During each period, if a new maximum (or minimum) is observed, the EP is updated with that value.

The α value represents the acceleration factor and is typically set initially to a value of 0.02. This factor increases by α each time a new EP is recorded. The rate will then quicken to a point where the SAR converges towards the price. To prevent it from getting too large, a maximum value for the acceleration factor is normally set to 0.20.

For additional detail, see wikipedia; there is also an extended SAR.

Normalized SAR indicator

Momentum Indicators

Function Name
PLUS_DM Plus Directional Movement
MINUS_DM Minus Directional Movement
PLUS_DI Plus Directional Indicator
MINUS_DI Minus Directional Indicator
DX Directional Movement Index
ADX Average Directional Movement Index
ADXR Average Directional Movement Index Rating
APO Absolute Price Oscillator
PPO Percentage Price Oscillator
AROON Aroon
AROONOSC Aroon Oscillator
BOP Balance Of Power
CCI Commodity Channel Index
CMO Chande Momentum Oscillator
MACD Moving Average Convergence/Divergence
MACDEXT MACD with controllable MA type
MACDFIX Moving Average Convergence/Divergence Fix 12/26
MFI Money Flow Index
MOM Momentum
RSI Relative Strength Index
STOCH Stochastic
STOCHF Stochastic Fast
STOCHRSI Stochastic Relative Strength Index
TRIX 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
ULTOSC Ultimate Oscillator
WILLR Williams' %R

Average Directional Movement Index (ADX)

The ADX combines of two other indicators, namely the positive and directional indicators (PLUS_DI and MINUS_DI), which in turn build on the positive and directional movement (PLUS_DM and MINUS_DM). For additional details see Wikipdia and Investopedia.

Plus/Minus Directional Movement (PLUS_DM/MINUS_DM)

For a price series $P_t$ with daily highs $P^H_t$ and daily lows $P^L_t$, the directional movement tracks the absolute size of price moves over a timeperiod T as follows:

$$\begin{align*} \text{Up}_t & =P^H_t-P^H_{t-T} \\ \text{Down}_t & = P^L_{t-T} - P^L_t\\ \text{PLUS_DM}_t & = \begin{cases} \text{Up}_t & \text{if Up}_t > \text{Down}_t \text{and Up}_t > 0 \\ 0 & \text{otherwise} \end{cases} \\ \text{MINUS_DM}_t & = \begin{cases} \text{Down}_t & \text{if Down}_t > \text{Up}_t \text{and Down}_t < 0 \\ 0 & \text{otherwise} \end{cases} \end{align*}$$

Plus/Minus Directional Index (PLUS_DI/MINUS_DI)

PLUS_DI and MINUS_DI are the simple moving average of PLUS_DM and MINUS_DM, respectively, each divided by the average true range (ATR, see below).

The smoothed moving average is calculated over the number of periods selected, and the average true range is a smoothed average of the true ranges.

Average directional movement index (ADX)

The ADX is the (simple) moving average of the absolute value of the difference between PLUS_DI and MINUS_DI, divided by their sum:

$$ \text{ADX}=100\times\text{SMA}(N)_t\left|\frac{\text{PLUS_DI}_t-\text{MINUS_DI}_t}{\text{PLUS_DI}_t+\text{MINUS_DI}_t}\right| $$
ADX Value Trend Strength
0-25 Absent or Weak Trend
25-50 Strong Trend
50-75 Very Strong Trend
75-100 Extremely Strong Trend

Visualize distribution

Average Directional Movement Index Rating

Averages the current ADX and the ADX T periods ago.

Absolute Price Oscillator (APO)

The absolute Price Oscillator (APO) is computed as the difference between two exponential moving averages (EMA) of price series, expressed as an absolute value. The EMA windows usually contain 26 and 12 data points, respectively.

Percentage Price Oscillator (PPO)

The Percentage Price Oscillator (APO) is computed as the difference between two exponential moving averages (EMA) of price series, expressed as a percentage value and thus comparable across assets. The EMA windows usually contain 26 and 12 data points, respectively.

Aroon Oscillator

Aroon Up/Down Indicator

The indicator measures the time between highs and the time between lows over a time period. It computes an AROON_UP and an AROON_DWN indicator as follows:

$$ \begin{align*} \text{AROON_UP}&=\frac{T-\text{Periods since T period High}}{T}\times 100\\ \text{AROON_DWN}&=\frac{T-\text{Periods since T period Low}}{T}\times 100 \end{align*} $$

Aroon Oscillator

The Aroon Oscillator is simply the difference between the Aroon Up and Aroon Down indicators.

Balance Of Power (BOP)

The Balance of Power (BOP) intends to measure the strength of buyers relative to sellers in the market by assessing the ability of each side to drive prices. It is computer as the difference between the close and the open price, divided by the difference between the high and the low price:

$$ \text{BOP}_t= \frac{P_t^\text{Close}-P_t^\text{Open}}{P_t^\text{High}-P_t^\text{Low}} $$

Commodity Channel Index (CCI)

The Commodity Channel Index (CCI) measures the difference between the current typical price, computed as the average of current low, high and close price and the historical average price. A positive (negative) CCI indicates that price is above (below) the historic average. When CCI is below zero, the price is below the hsitoric average. It is computed as:

$$ \begin{align*} \bar{P_t}&=\frac{P_t^H+P_t^L+P_t^C}{3}\\ \text{CCI}_t & =\frac{\bar{P_t} - \text{SMA}(T)_t}{0.15\sum_{t=i}^T |\bar{P_t}-\text{SMA}(N)_t|/T} \end{align*} $$

Moving Average Convergence/Divergence (MACD)

Moving Average Convergence Divergence (MACD) is a trend-following (lagging) momentum indicator that shows the relationship between two moving averages of a security’s price. It is calculated by subtracting the 26-period Exponential Moving Average (EMA) from the 12-period EMA.

The TA-Lib implementation returns the MACD value and its signal line, which is the 9-day EMA of the MACD. In addition, the MACD-Histogram measures the distance between the indicator and its signal line.

Chande Momentum Oscillator (CMO)

The Chande Momentum Oscillator (CMO) intends to measure momentum on both up and down days. It is calculated as the difference between the sum of gains and losses over at time period T, divided by the sum of all price movement over the same period. It oscillates between +100 and -100.

Not included due to high correlation with PPO.

Money Flow Index

The Money Flow Index (MFI) incorporates price and volume information to identify overbought or oversold conditions. The indicator is typically calculated using 14 periods of data. An MFI reading above 80 is considered overbought and an MFI reading below 20 is considered oversold.

Relative Strength Index

RSI compares the magnitude of recent price changes across stocks to identify stocks as overbought or oversold. A high RSI (usually above 70) indicates overbought and a low RSI (typically below 30) indicates oversold. It first computes the average price change for a given number (often 14) of prior trading days with rising and falling prices, respectively as $\text{up}_t$ and $\text{down}_t$. Then, the RSI is computed as: $$ \text{RSI}_t=100-\frac{100}{1+\frac{\text{up}_t}{\text{down}_t}} $$

Stochastic RSI (STOCHRSI)

The Stochastic Relative Strength Index (STOCHRSI) is based on the RSI just described and intends to identify crossovers as well as overbought and oversold conditions. It compares the distance of the current RSI to the lowest RSI over a given time period T to the maximum range of values the RSI has assumed for this period. It is computed as follows:

$$ \text{STOCHRSI}_t= \frac{\text{RSI}_t-\text{RSI}_t^L(T)}{\text{RSI}_t^H(T)-\text{RSI}_t^L(T)} $$

The TA-Lib implementation offers more flexibility than the original "Unsmoothed stochastic RSI" version by Chande and Kroll (1993). To calculate the original indicator, keep the timeperiod and fastk_period equal.

The return value fastk is the unsmoothed RSI. The fastd_period is used to compute a smoothed STOCHRSI, which is returned as fastd. If you do not care about STOCHRSI smoothing, just set fastd_period to 1 and ignore the fastd output.

Reference: "Stochastic RSI and Dynamic Momentum Index" by Tushar Chande and Stanley Kroll Stock&Commodities V.11:5 (189-199)

Stochastic (STOCH)

A stochastic oscillator is a momentum indicator comparing a particular closing price of a security to a range of its prices over a certain period of time. Stochastic oscillators are based on the idea that closing prices should confirm the trend.

For stochastic (STOCH), there are four different lines: FASTK, FASTD, SLOWK and SLOWD. The D is the signal line usually drawn over its corresponding K function.

$$ \begin{align*} & K^\text{Fast}(T_K) & = &\frac{P_t-P_{T_K}^L}{P_{T_K}^H-P_{T_K}^L}* 100 \\ & D^\text{Fast}(T_{\text{FastD}}) & = & \text{MA}(T_{\text{FastD}})[K^\text{Fast}]\\ & K^\text{Slow}(T_{\text{SlowK}}) & = &\text{MA}(T_{\text{SlowK}})[K^\text{Fast}]\\ & D^\text{Slow}(T_{\text{SlowD}}) & = &\text{MA}(T_{\text{SlowD}})[K^\text{Slow}] \end{align*} $$

The $P_{T_K}^L$, $P_{T_K}^H$, and $P_{T_K}^L$ are the extreme values among the last $T_K$ period. $K^\text{Slow}$ and $D^\text{Fast}$ are equivalent when using the same period.

Ultimate Oscillator (ULTOSC)

The Ultimate Oscillator (ULTOSC), developed by Larry Williams, measures the average difference of the current close to the previous lowest price over three time frames (default: 7, 14, and 28) to avoid overreacting to short-term price changes and incorporat short, medium, and long-term market trends. It first computes the buying pressure, $\text{BP}_t$, then sums it over the three periods $T_1, T_2, T_3$, normalized by the True Range ($\text{TR}_t$. $$ \begin{align*} \text{BP}_t & = P_t^\text{Close}-\min(P_{t-1}^\text{Close}, P_t^\text{Low})\\ \text{TR}_t & = \max(P_{t-1}^\text{Close}, P_t^\text{High})-\min(P_{t-1}^\text{Close}, P_t^\text{Low}) \end{align*} $$

ULTOSC is then computed as a weighted average over the three periods as follows: $$ \begin{align*} \text{Avg}_t(T) & = \frac{\sum_{i=0}^{T-1} \text{BP}_{t-i}}{\sum_{i=0}^{T-1} \text{TR}_{t-i}}\\ \text{ULTOSC}_t & = 100*\frac{4\text{Avg}_t(7) + 2\text{Avg}_t(14) + \text{Avg}_t(28)}{4+2+1} \end{align*} $$

Williams' %R (WILLR)

Williams %R, also known as the Williams Percent Range, is a momentum indicator that moves between 0 and -100 and measures overbought and oversold levels to identify entry and exit points. It is similar to the Stochastic oscillator and compares the current closing price $P_t^\text{Close}$ to the range of highest ($P_T^\text{High}$) and lowest ($P_T^\text{Low}$) prices over the last T periods (typically 14). The indicators is computed as:

$$ \text{WILLR}_t = \frac{P_T^\text{High}-P_t^\text{Close}}{P_T^\text{High}-P_T^\text{Low}} $$

Volume Indicators

Function Name
AD Chaikin A/D Line
ADOSC Chaikin A/D Oscillator
OBV On Balance Volume

Chaikin A/D Line

The Chaikin Advance/Decline or Accumulation/Distribution Line (AD) is a volume-based indicator designed to measure the cumulative flow of money into and out of an asset. The indicator assumes that the degree of buying or selling pressure can be determined by the location of the close, relative to the high and low for the period. There is buying (sellng) pressure when a stock closes in the upper (lower) half of a period's range. The intention is to signal a change in direction when the indicator diverges from the security price.

The Accumulation/Distribution Line is a running total of each period's Money Flow Volume. It is calculated as follows:

  1. The Money Flow Multiplier (MFI) is the relationship of the close to the high-low range:
  2. The MFI is multiplied by the period's volume $V_t$ to come up with a Money Flow Volume (MFV).
  3. A running total of the Money Flow Volume forms the Accumulation Distribution Line: $$ \begin{align*} &\text{MFI}_t&=\frac{P_t^\text{Close}-P_t^\text{Low}}{P_t^\text{High}-P_t^\text{Low}}\\ &\text{MFV}_t&=\text{MFI}_t \times V_t\\ &\text{AD}_t&=\text{AD}_{t-1}+\text{MFV}_t \end{align*} $$

Divide by average volume to normalize across assets.

Chaikin A/D Oscillator (ADOSC)

The Chaikin A/D Oscillator (ADOSC) is the Moving Average Convergence Divergence indicator (MACD) applied to the Chaikin A/D Line. The Chaikin Oscillator intends to predict changes in the Accumulation/Distribution Line.

It is computed as the difference between the 3-day exponential moving average and the 10-day exponential moving average of the Accumulation/Distribution Line.

On Balance Volume (OBV)

The On Balance Volume indicator (OBV) is a cumulative momentum indicator that relates volume to price change. It assumes that OBV changes precede price changes because smart money can be seen flowing into the security by a rising OBV. When the public then moves into the security, both the security and OBV will rise.

The current OBV is computed by adding (subtracting) the current volume to the last OBV if the security closes higher (lower) than the previous close.

$$ \text{OBV}_t = \begin{cases} \text{OBV}_{t-1}+V_t & \text{if }P_t>P_{t-1}\\ \text{OBV}_{t-1}-V_t & \text{if }P_t<P_{t-1}\\ \text{OBV}_{t-1} & \text{otherwise} \end{cases} $$

Volatility Indicators

Function Name
TRANGE True Range
ATR Average True Range
NATR Normalized Average True Range

ATR

The Average True Range indicator (ATR) shows volatility of the market. It was introduced by Welles Wilder (1978) and has been used as a component of numerous other indicators since. It aims to anticipate changes in trend such that the higher its value, the higher the probability of a trend change; the lower the indicator’s value, the weaker the current trend.

It is computed as the simple moving average for a period T of the True Range (TRANGE), which measures volatility as the absolute value of the largest recent trading range: $$ \text{TRANGE}_t = \max\left[P_t^\text{High} - P_t^\text{low}, \left| P_t^\text{High} - P_{t-1}^\text{Close}\right|, \left| P_t^\text{low} - P_{t-1}^\text{Close}\right|\right] $$

Compute normalized version of ATR using rolling mean of price

NATR

The Normalized Average True Range (NATR) is a normalized version of the ATR computed as follows:

$$ \text{NATR}_t = \frac{\text{ATR}_t(T)}{P_t^\text{Close}} * 100 $$

Normalization make the ATR function more relevant in the folllowing scenarios:

Not included in data due to high correlation with ATR.

Rolling Factor Betas

Size proxy

Persist results